#!/bin/bash

name="/dev/[DEVICE_NAME]"
mountPoint="[MOUNT_POINT]"

echo -n "Configuring disk \"$name\" ... "
details=$(fdisk -l $name 2>&1)
if [[ $details == *"Disk $name doesn't contain a valid partition table"* ]]; then
  echo -e "n\np\n1\n\n\nw" | fdisk $name &> /dev/null
  mkfs -t ext4 $name"1" &> /dev/null
  if [ ! -d "$mountPoint" ]; then
    mkdir $mountPoint
  fi
  chmod -R go+rw $mountPoint
  mount $name"1" $mountPoint
  uuid=$(blkid >&1)
  exp=$name"1: UUID=\"([^\"]+)"
  [[ $uuid =~ $exp ]]
  uuid=${BASH_REMATCH[1]}
  echo "UUID=$uuid $mountPoint ext4 defaults 1 2" >> "/etc/fstab"
  echo "done."
else
  echo "skipped."
fi

# Verify:
# - sudo grep -i "Attached SCSI disk" /var/log/syslog
# - sudo lshw -C disk
# - sudo fdisk -l
# - sudo lsblk -o NAME,TYPE,FSTYPE,SIZE,MOUNTPOINT,LABEL,UUID